﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLTN.Entities;
using DLTN.Data;
using System.Data;

public partial class SubChuyenMuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["LoaiChuyenMuc"] != null && Request.Params["Id"] != null)
            {
                H_HiddenIdLoai.Value = Request.Params["LoaiChuyenMuc"];
                H_HiddenIDSub.Value = Request.Params["Id"];
                HienThiChuyenMuc(Convert.ToInt32(H_HiddenIdLoai.Value));
                HienThiSubChuyenMuc(Convert.ToInt32(H_HiddenIDSub.Value));
                HienThiTinTuc();
                Pager1.CurrentIndex = 1;
                HienThiTinTucKhac(Pager1.CurrentIndex);
            }
        }
    }

    private void HienThiChuyenMuc(int Id)
    {
        LoaiChuyenMuc ent_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(Id);
        litTenLoaiChuyenMuc.Text = ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToUpper();
        if (ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToUpper() == "DỊCH VỤ")
            linkSubChuyenMuc.NavigateUrl = "~/DichVu.aspx";
        if (ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToUpper() == "MEDIA")
            linkSubChuyenMuc.NavigateUrl = "~/Media.aspx";
        if (ent_LoaiChuyenMuc.TenLoaiChuyenMuc.ToUpper() == "GIỚI THIỆU")
            linkSubChuyenMuc.NavigateUrl = "~/GioiThieu.aspx";
    }

    private void HienThiSubChuyenMuc(int Id)
    {
        DLTN.Entities.SubChuyenMuc ent_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(Id);
        litTenSubChuyenMuc.Text = ent_SubChuyenMuc.TenSubChuyenMuc.ToUpper();
        ltrTitle.Text = ent_SubChuyenMuc.TenSubChuyenMuc;
    }

    private void HienThiTinTuc()
    {
        string strHTML = "";
        int TotalRows = 0;
        string WhereClause = "1=1";
        //TList<DLTN.Entities.SubChuyenMuc> TList_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged("TenSubChuyenMuc like N'%" + Common.TinTuc + "%'", "Id", 0, 1, out TotalRows);
        WhereClause += "and TrangThai = 1 and IdSubChuyenMuc = " + H_HiddenIDSub.Value;

        TList<ChuyenMuc> TList_ChuyenMuc_TinTuc = DataRepository.ChuyenMucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 5, out TotalRows);

        if (TList_ChuyenMuc_TinTuc.Count > 0)
        {
            lblNgayCapNhat.Text = Common.StandardStringjquery(TList_ChuyenMuc_TinTuc[0].NgayCapNhat.ToString("''dd/MM/yyyy, HH:mm 'GMT+7'"));

            // Hiển thị tin thứ 1 bên left
            strHTML += "<div id=\"tin-left\">";
            strHTML += "<a href=\"../../../" + "ChuyenMuc/" + TList_ChuyenMuc_TinTuc[0].Id + "/" + TList_ChuyenMuc_TinTuc[0].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc_TinTuc[0].TieuDe) + ".aspx\">";
            strHTML += "<img src=\"../../../Upload/ChuyenMuc/" + TList_ChuyenMuc_TinTuc[0].AnhDaiDien + "\" width=\"380px\" height=\"250px\" border=\"0\"/></a>";
            strHTML += "<div id=\"tin-left-text\">";
            strHTML += "<a href=\"../../../" + "ChuyenMuc/" + TList_ChuyenMuc_TinTuc[0].Id + "/" + TList_ChuyenMuc_TinTuc[0].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc_TinTuc[0].TieuDe) + ".aspx\">";
            strHTML += TList_ChuyenMuc_TinTuc[0].TieuDe + "</a>&nbsp;" + TList_ChuyenMuc_TinTuc[0].TomTat + "</div>";
            strHTML += "</div>";

            // Hiển thị 4 tin còn lại bên right
            strHTML += "<div id=\"tin-right-wrapper\" >";
            strHTML += "<h2>TIN MỚI</h2>";
            for (int i = 1; i < TList_ChuyenMuc_TinTuc.Count; i++)
            {
                if (i == 1)
                {
                    strHTML += "<div id=\"tin-right\">";
                }
                else
                {
                    strHTML += "<div id=\"tin-right\" style=\"background: url('Images/dltn/rightNaviTitBg-new.png') no-repeat;\">";
                }
                strHTML += "<div id=\"tin-right-img\" >";
                strHTML += "<a href=\"../../../" + "ChuyenMuc/" + TList_ChuyenMuc_TinTuc[i].Id + "/" + TList_ChuyenMuc_TinTuc[i].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc_TinTuc[i].TieuDe) + ".aspx\">";
                strHTML += "<img src=\"../../../" + "Upload/ChuyenMuc/" + TList_ChuyenMuc_TinTuc[i].AnhDaiDien + "\" width=\"100px\" height=\"80px\" border=\"0\"/></a>";
                strHTML += "</div>";

                strHTML += "<div id=\"tin-right-text\">";
                strHTML += "<a href=\"../../../" + "ChuyenMuc/" + TList_ChuyenMuc_TinTuc[i].Id + "/" + TList_ChuyenMuc_TinTuc[i].IdSubChuyenMuc + "/" + GetTieuDe(TList_ChuyenMuc_TinTuc[i].TieuDe) + ".aspx\">";
                strHTML += TList_ChuyenMuc_TinTuc[i].TieuDe + "</a>&nbsp;";// +ChuanHoaChuoi(TList_ChuyenMuc_TinTuc[i].TomTat.ToString());

                if (TList_ChuyenMuc_TinTuc[i].TieuDe.Length > 38)
                    strHTML += formatString(TList_ChuyenMuc_TinTuc[i].TomTat.ToString(), 49);
                else
                    strHTML += formatString(TList_ChuyenMuc_TinTuc[i].TomTat.ToString(), 55);

                
                strHTML += "</div>";

                strHTML += "</div>";
            }
                      

            strHTML += "</div>";

        }
        else
        {
            strHTML = "<div style=\"width: 250px;min-height:100px;height: auto;\">";
            strHTML += "Dữ liệu chưa được cập nhật</div>";
        }

        ltrMainset.Text = strHTML;
    }

    private void HienThiTinTucKhac(int p)
    {
        try
        {
            int TotalRows = 0;
            string WhereClause = "1=1 ";

            //TList<DLTN.Entities.SubChuyenMuc> TList_Other_SubChuyenMuc = DataRepository.SubChuyenMucProvider.GetPaged("TenSubChuyenMuc like N'%" + Common.TinTuc + "%'", "Id", 0, 1, out TotalRows);
            WhereClause += "and TrangThai = 1 and IdSubChuyenMuc = " + H_HiddenIDSub.Value;

            TList<ChuyenMuc> TList_ChuyenMuc_OtherTinTuc = DataRepository.ChuyenMucProvider.GetPaged(WhereClause, "NgayCapNhat DESC", 0, 5, out TotalRows);

            string otherClause = "1=1 ";
            //otherClause += "and TrangThai = 1 and IdSubChuyenMuc = " + TList_Other_SubChuyenMuc[0].Id + " and Id not in(16)";
            otherClause += "and TrangThai = 1 and IdSubChuyenMuc = " + H_HiddenIDSub.Value + " and Id not in(";

            for (int i = 0; i < TList_ChuyenMuc_OtherTinTuc.Count - 1; i++)
            {
                otherClause += TList_ChuyenMuc_OtherTinTuc[i].Id + ", ";
            }

            otherClause += TList_ChuyenMuc_OtherTinTuc[TList_ChuyenMuc_OtherTinTuc.Count - 1].Id + ")";

            DataSet dsOther = DataRepository.Provider.ExecuteDataSet("ChuyenMuc_GetPaged", otherClause, "Id DESC", p - 1, Pager1.PageSize);
            
            dtlOthers.DataSource = dsOther.Tables[0];
            dtlOthers.DataBind();

            try
            {
                TotalRows = Convert.ToInt32(dsOther.Tables[1].Rows[0][0]);
            }
            catch (Exception) {}

            Pager1.ItemCount = TotalRows;

            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception) { }
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        HienThiTinTucKhac(currnetPageIndex);
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }
}